class SpecialLatex
	@command_map = {
		"alpha" => "α",
		"beta" => "β",
		"gamma" => "γ",
		"delta" => "δ",
		"epsilon" => "ε",
		"zeta" => "ζ",
		"eta" => "η",
		"theta" => "θ",
		"iota" => "ι",
		"kappa" => "κ",
		"lambda" => "λ",
		"mu" => "μ",
		"nu" => "ν",
		"xi" => "ξ",
		"pi" => "π",
		"rho" => "ρ",
		"sigma" => "σ",
		"tau" => "τ",
		"upsilon" => "υ",
		"phi" => "φ",
		"chi" => "χ",
		"psi" => "ψ",
		"omega" => "ω",
		"Gamma" => "Γ",
		"Lambda" => "Λ",
		"Sigma" => "Σ",
		"Psi" => "Ψ",
		"Delta" => "Δ",
		"Xi" => "Ξ",
		"Upsilon" => "Υ",
		"Omega" => "Ω",
		"Theta" => "Θ",
		"Pi" => "Π",
		"Phi" => "Φ",
		"ae" => "æ",
		"AE" => "Æ",
		"oe" => "œ",
		"OE" => "Œ",
		"aa" => "å",
		"AA" => "Å",
		"o" => "ø",
		"O" => "Ø",
		"ss" => "ß",
		"euro" => "€",
		"textdegree" => "°",
		"%" => "%",
		"$" => "$",
		"{" => "{",
		"}" => "}",
		"_" => "_",
		"P" => "¶",
		"dag" => "†",
		"ddag" => "‡",
		"textbar" => "|",
		"textgreater" => ">",
		"textless" => "<",
		"textendash" => "–",
		"textemdash" => "—",
		"texttrademark" => "™",
		"textregistered" => "®",
		"textendash" => "–",
		"textemdash" => "—",
		"textexclamdown" => "¡",
		"textquestiondown" => "¿",
		"pounds" => "£",
		"#" => "#",
		"&" => "&",
		"S" => "§",
		"copyright" => "©",
		"alpha" => "α",
		"l" => "ł"
	}

	@ascii_map = {
		"`a" => "à",
		"`e" => "è",
		"`i" => "ì",
		"`o" => "ò",
		"`u" => "ù",
		"`A" => "À",
		"`E" => "È",
		"`I" => "Ì",
		"'a" => "á",
		"'e" => "é",
		"'i" => "í",
		"'o" => "ó",
		"'u" => "ú",
		"'A" => "Á",
		"'E" => "É",
		"'I" => "v",
		"'O" => "Ó",
		"'U" => "Ú",
		"^a" => "â",
		"^e" => "ê",
		"^i" => "î",
		"^o" => "ô",
		"^u" => "û",
		"^A" => "Â",
		"^E" => "Ê",
		"^I" => "Î",
		"^O" => "Ô",
		"^U" => "Û",
		"\"a" => "ä",
		"\"e" => "ë",
		"\"i" => "ï",
		"\"o" => "ö",
		"\"u" => "ü",
		"\"y" => "ÿ",
		"\"A" => "Ä",
		"\"E" => "Ë",
		"\"I" => "Ï",
		"\"O" => "Ö",
		"\"U" => "Ü",
		"\"Y" => "Ÿ",
		"Ho" => "ő",
		"HO" => "Ő",
		"Hu" => "ű",
		"HU" => "Ű",
		"~a" => "ã",
		"~n" => "ñ",
		"~o" => "õ",
		"~A" => "Ã",
		"~N" => "Ñ",
		"~O" => "Õ",
		"cc" => "ç",
		"cC" => "Ç",
		"ka" => "ą",
		"kA" => "Ą",
		"ki" => "į",
		"kI" => "Į",
		"ku" => "ų",
		"kU" => "Ų",
		"ke" => "ę",
		"kE" => "Ę",
		"ko" => "ǫ",
		"kO" => "Ǫ",
		"=a" => "ā",
		"=A" => "Ā",
		"=e" => "ē",
		"=E" => "Ē",
		"=i" => "ī",
		"=I" => "Ī",
		"=o" => "ō",
		"=O" => "Ō",
		"=u" => "ū",
		"=U" => "Ū",
		"=y" => "ȳ",
		"=Y" => "Ȳ",
		"=æ" => "ǣ",
		"=Æ" => "Ǣ",
		"=g" => "ḡ",
		"=G" => "Ḡ",
		"bb" => "ḇ",
		"bB" => "Ḇ",
		"bd" => "ḏ",
		"bD" => "Ḏ",
		"bh" => "ẖ",
		"bk" => "ḵ",
		"bK" => "Ḵ",
		"bl" => "ḻ",
		"bL" => "Ḻ",
		"bn" => "ṉ",
		"bN" => "Ṉ",
		"br" => "ṟ",
		"bR" => "Ṟ",
		"bt" => "ṯ",
		"bT" => "Ṯ",
		"bz" => "ẕ",
		"bZ" => "Ẕ",
		"ra" => "å",
		"rA" => "Å",
		"ua" => "ă",
		"uA" => "Ă",
		"ue" => "ĕ",
		"uE" => "Ĕ",
		"ui" => "ĭ",
		"uI" => "Ĭ",
		"uo" => "ŏ",
		"uO" => "Ŏ",
		"uu" => "ŭ",
		"uU" => "Ŭ"
	}

	@connecting_map = {
		"`" => "\u0300",
		"'" => "\u0301",
		"^" => "\u0302",
		"\"" => "\u0308",
		"H" => "\u030B",
		"~" => "\u0303",
		"c" => "\u0327",
		"k" => "\u0328",
		"=" => "\u0304",
		"b" => "\u0331",
		"." => "\u0307",
		"d" => "\u0323",
		"r" => "\u030A",
		"u" => "\u0306",
		"v" => "\u030C"
	}

	@command_regex = /\\(.+?)(\{\s*\}|(\s|$)|\{(.+?)})/

	def self.convert(str)
		str.gsub(@command_regex) do |s|
			if @command_map.key?($1) then
				@command_map[$1]
			elsif not $4.empty? then
				combined = $1 + $4
				if @ascii_map.key?(combined) then
					@ascii_map[combined]
				elsif @connecting_map.key?($1) then
					$4 + @connecting_map[$1]
				else
					s
				end
			else
				s
			end
		end
	end
end